package com.jdd.fintech.morse.plugin.util;

import org.apache.commons.httpclient.util.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * @description: 日期工具类
 * @credit: 2022-03-28 14:38
 * @author: fengcheng
 **/
public class PluginDateUtils {

    private static final Logger logger = LoggerFactory.getLogger(DateUtil.class);

    public static final String str_yyyyMMddHHmmsssss = "yyyyMMddHHmmssSSS";
    public static final String str_yyyyMMddHHmmss = "yyyyMMddHHmmss";
    public static final String str_yyyy_MM_dd_HH_mm_ss = "yyyy-MM-dd HH:mm:ss";
    public static final String str_yyyyMMdd_HH_ss = "yyyyMMdd HH:mm:ss";
    public static final String str_yyyy_MM_dd = "yyyy-MM-dd";
    public static final String str_yyyy_MM = "yyyy-MM";
    public static final String str_yyyyMMDD = "yyyyMMdd";
    public static final String str_HHmmss = "HHmmss";
    public static final String str_HHmmsssss = "HHmmsssss";
    public static final String str_yyyyYearMMMonthddDay = "yyyy年MM月dd日";

    /**
     * 将日期格式化为字符串
     *
     * @param date    日期
     * @param pattern 格式
     * @return 格式化后的日期串
     */
    public static String date2Str(Date date, String pattern) {
        SimpleDateFormat yyyy_MM_dd = new SimpleDateFormat(pattern);
        return yyyy_MM_dd.format(date);
    }

    /**
     * 获得当前时间的<code>java.util.Date</code>对象
     *
     * @return
     */
    public static Date now() {
        return new Date();
    }

    /**
     * 获得当前时间 精确到秒
     *
     * @return
     */
    public static String getNow() {
        SimpleDateFormat yyyy_MM_dd_HH_mm_ss = new SimpleDateFormat(str_yyyy_MM_dd_HH_mm_ss);
        return yyyy_MM_dd_HH_mm_ss.format(new Date());
    }

    /**
     * 获得当前日期时间
     *
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String getYYYY_MM_dd_HH_mm_ss() {
        SimpleDateFormat yyyy_MM_dd_HH_mm_ss = new SimpleDateFormat(str_yyyy_MM_dd_HH_mm_ss);
        return yyyy_MM_dd_HH_mm_ss.format(now());
    }

    /**
     * 获得当前日期时间精确到毫秒
     *
     * @return yyyyMMddHHmmssSSS
     */
    public static String getYYYYMMddHHmmsssss() {
        SimpleDateFormat yyyyMMddHHmmsssss = new SimpleDateFormat(str_yyyyMMddHHmmsssss);
        return yyyyMMddHHmmsssss.format(now()).replace(" ", "" + "");
    }

    /**
     * 获得当前日期时间
     *
     * @return yyyyMMddHHmmss
     */
    public static String getYYYYMMddHHmmss() {
        SimpleDateFormat yyyyMMddHHmmss = new SimpleDateFormat(str_yyyyMMddHHmmss);
        return yyyyMMddHHmmss.format(now());
    }

    /**
     * 获得当前的日期
     *
     * @return
     */
    public static String getYYYYMMdd() {
        SimpleDateFormat yyyyMMdd = new SimpleDateFormat(str_yyyyMMDD);
        return yyyyMMdd.format(now());
    }

    /**
     * 获得当前的时间
     *
     * @return
     */
    public static String getHHmmss() {
        SimpleDateFormat hhmmss = new SimpleDateFormat(str_HHmmss);
        return hhmmss.format(now());
    }

    /**
     * 获得当前的时间
     *
     * @return
     */
    public static String getHHmmsssss() {
        SimpleDateFormat hhmmss = new SimpleDateFormat(str_HHmmsssss);
        return hhmmss.format(now());
    }

    /**
     * 获得当前日期时间的年-月-日
     *
     * @return yyyy-MM-dd
     */
    public static String getYYYY_MM_dd() {
        SimpleDateFormat yyyy_MM_dd = new SimpleDateFormat(str_yyyy_MM_dd);
        return yyyy_MM_dd.format(now()).substring(0, 10);
    }

    /**
     * 获得当前日期时间的年
     *
     * @return yyyy-MM
     */
    public static String getYY() {
        SimpleDateFormat yyyy_MM = new SimpleDateFormat(str_yyyy_MM);
        return yyyy_MM.format(now()).substring(2, 4);
    }

    /**
     * 获得当前日期时间的年-月
     *
     * @return yyyy-MM
     */
    public static String getYYYY_MM() {
        SimpleDateFormat yyyy_MM = new SimpleDateFormat(str_yyyy_MM);
        return yyyy_MM.format(now()).substring(0, 7);
    }

    /**
     * 获取两个日期的相差天数
     * @param startTime
     * @param endTime
     * @return
     */
    public static int getIntervalDays(Date startTime, Date endTime){
        String startTimeStr = formatDate(startTime, "yyyy-MM-dd") + " 00:00:00";
        String endTimeStr = formatDate(endTime, "yyyy-MM-dd") + " 00:00:00";

        startTime = parseStr2Date(startTimeStr);
        endTime = parseStr2Date(endTimeStr);

        long start = startTime.getTime();
        long end = endTime.getTime();
        return (int)((end - start) / (1000 * 3600 * 24));
    }

    /**
     * yyyy-MM-dd HH:mm:ss 转成date
     *
     * @param dateStr
     * @return
     */
    public static Date parseStr2Date(String dateStr) {
        Date date = null;
        try {
            SimpleDateFormat yyyy_MM_dd_HH_mm_ss = new SimpleDateFormat(str_yyyy_MM_dd_HH_mm_ss);
            date = yyyy_MM_dd_HH_mm_ss.parse(dateStr);
        } catch (ParseException e) {

        }
        return date;
    }

    /**
     * 按指定格式将字符串转换为日期
     *
     * @param dateStr
     *            日期串
     * @param pattern
     *            格式
     * @return 日期
     * @throws Exception
     *             异常
     */
    public static Date str2Date(String dateStr, String pattern) throws Exception {
        if (null == dateStr || dateStr.length() < 1) {
            return null;
        }
        if (null == pattern) {
            pattern = str_yyyyMMddHHmmss;
        }
        SimpleDateFormat format = new SimpleDateFormat();
        format.applyPattern(pattern);
        return format.parse(dateStr);
    }

    /**
     * date 转成 yyyy-MM-dd HH:mm:ss
     *
     * @param date
     * @return
     */
    public static String parseDate2Str(Date date) {
        SimpleDateFormat yyyy_MM_dd_HH_mm_ss = new SimpleDateFormat(str_yyyy_MM_dd_HH_mm_ss);
        return yyyy_MM_dd_HH_mm_ss.format(date);

    }



    public static String formatDate(Date date, String format) {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
            return simpleDateFormat.format(date);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 根据时间获得毫秒数
     */
    public static long parseDate2Long(Date date) {
        return date.getTime();
    }

    /**
     * 根据时间获得毫秒数
     */
    public static long parseDate2Long(String dateStr) {
        return parseStr2Date(dateStr).getTime();

    }

    /**
     * 当前时间与dateStr相差几天
     */
    public static float compareNow(String dateStr) {
        Long start = new Date().getTime();
        Long end = null;
        try {
            SimpleDateFormat yyyy_MM_dd_HH_mm_ss = new SimpleDateFormat(str_yyyy_MM_dd_HH_mm_ss);
            end = yyyy_MM_dd_HH_mm_ss.parse(dateStr).getTime();
        } catch (ParseException e) {

        }
        return (start - end) / (1000 * 3600 * 24);

    }

    /**
     * 当前时间与dateStr相差几天
     */
    public static int compareNow(Date date) {
        int days = 0;
        try {
            Calendar cal1 = Calendar.getInstance();
            SimpleDateFormat yyyy_MM_dd = new SimpleDateFormat(str_yyyy_MM_dd);
            cal1.setTime(yyyy_MM_dd.parse(yyyy_MM_dd.format(date)));
            Calendar cal2 = Calendar.getInstance();
            cal2.setTime(yyyy_MM_dd.parse(yyyy_MM_dd.format(now())));
            long l = cal1.getTimeInMillis() - cal2.getTimeInMillis();
            days = new Long(l / (1000 * 60 * 60 * 24)).intValue();
        } catch (Exception e) {

        }
        return days;
    }

    /**
     * 当前时间的前N秒的时间点
     */
    public static Date getXminSecAgo(String dateTime, int second) {
        Calendar calendar = null;
        try {
            calendar = Calendar.getInstance();//得到日历
            SimpleDateFormat yyyy_MM_dd_HH_mm_ss = new SimpleDateFormat(str_yyyy_MM_dd_HH_mm_ss);
            calendar.setTime(yyyy_MM_dd_HH_mm_ss.parse(dateTime));
            calendar.add(Calendar.SECOND, -second);
        } catch (Exception e) {

        }
        return calendar.getTime();
    }

    /**
     * 当前时间的后N秒的时间点
     */
    public static Date getXminSecAfter(String dateTime, int second) {
        Calendar calendar = null;
        try {
            calendar = Calendar.getInstance();//得到日历
            SimpleDateFormat yyyy_MM_dd_HH_mm_ss = new SimpleDateFormat(str_yyyy_MM_dd_HH_mm_ss);
            calendar.setTime(yyyy_MM_dd_HH_mm_ss.parse(dateTime));
            calendar.add(Calendar.SECOND, +second);
        } catch (Exception e) {

        }
        return calendar.getTime();
    }

    /**
     * 获得当前日期时间的 HH:mm:ss
     *
     * @return
     */
    public static String getHH_mm_ss() {
        SimpleDateFormat yyyy_MM_dd_HH_mm_ss = new SimpleDateFormat(str_yyyy_MM_dd_HH_mm_ss);
        return yyyy_MM_dd_HH_mm_ss.format(now()).substring(11);
    }

    /**
     * 月份格式化，如果是本月，则显示为本月；如果是本年度，则直接显示月份，否则显示年度-月份
     *
     * @param date
     * @return
     */
    public static String formatMonthDesc(Date date) {
        if (date == null) {
            return "";
        }
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        int currentMonth = c.get(Calendar.MONTH) + 1;
        int currentYear = c.get(Calendar.YEAR);

        c.setTime(date);
        int tradeMonth = c.get(Calendar.MONTH) + 1;
        int tradeYead = c.get(Calendar.YEAR);
        if (currentYear == tradeYead) {
            if (tradeMonth == currentMonth) {
                return "本月";
            } else {
                return tradeMonth + "月";
            }
        } else {
            return tradeYead + "年" + tradeMonth + "月";
        }
    }

    /**
     * 日期格式化，格式化为：今天 昨天
     *
     * @param date
     * @return
     */
    public static String formatDayDesc(Date date) {
        //DateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (date == null) {
            return "";
        }
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        //System.out.println("当前时间：" + formate.format(c.getTime()));
        long currentDay = c.getTimeInMillis();
        int currentYear = c.get(Calendar.YEAR);

        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        //System.out.println("交易时间：" + formate.format(c.getTime()));
        long tradeDay = c.getTimeInMillis();
        int tradeYear = c.get(Calendar.YEAR);

        long time = currentDay - tradeDay;
        // 计算小时
        long hour = time / (3600 * 1000);
        if (hour < 24) {
            DateFormat dateFormat = new SimpleDateFormat("今天 HH:mm");
            return dateFormat.format(date);
        } else if (hour >= 24 && hour < 48) {
            DateFormat dateFormat = new SimpleDateFormat("昨天 HH:mm");
            return dateFormat.format(date);
        } else {
            // 本年度内
            if (currentYear == tradeYear) {
                DateFormat dateFormat = new SimpleDateFormat("MM-dd");
                return dateFormat.format(date);
            } else {
                // 非本年度内
                DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                return dateFormat.format(date);
            }
        }
    }


    /**
     * 获取当前日期前一个月的日期
     *
     * @return
     */
    public static String getbeforeMonth() {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.MONTH, c.get(Calendar.MONTH) - 1);
        Date day = c.getTime();
        String str = new SimpleDateFormat(str_yyyyMMDD).format(day);
        return str;
    }

    /**
     * 获取当前日期前一年的日期
     *
     * @return
     */
    public static String getbeforeYear() {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, c.get(Calendar.YEAR) - 1);
        Date day = c.getTime();
        String str = new SimpleDateFormat(str_yyyyMMDD).format(day);
        return str;
    }

    /**
     * 获取传入日期增减天数后的日期
     *
     * @return
     */
    public static String getAddDate(String date, int num, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(sdf.parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        calendar.add(Calendar.DATE, num);
        return sdf.format(calendar.getTime());
    }

    /**
     * 获取传入日期增减月数后的日期
     *
     * @param date
     * @param num
     * @return
     */
    public static String getAddMonth(String date, int num, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(sdf.parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        calendar.add(Calendar.MONTH, num);
        return sdf.format(calendar.getTime());
    }

    /**
     * 获取传入日期增减年数后的日期
     *
     * @param date
     * @param num
     * @return
     */
    public static String getAddYear(String date, int num, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(sdf.parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        calendar.add(Calendar.YEAR, num);
        return sdf.format(calendar.getTime());
    }

    /**
     * 根据指定格式Format日期时间
     *
     * @param dateTime
     * @param sourceFormat
     * @param resultFormat
     * @return
     */
    public static String formatDateTime(String dateTime, String sourceFormat, String resultFormat) {
        String res = null;
        Date date = null;
        try {
            date = new SimpleDateFormat(sourceFormat).parse(dateTime);
            res = new SimpleDateFormat(resultFormat).format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    /**
     * 获取时间上下一个间隔分钟后的时间
     * @param dateTime
     * @param minutes
     * @return
     */
    public static Date getNextIntervalMinuteTime(Date dateTime, int minutes){
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.MINUTE, minutes);
        return c.getTime();
    }

    /**
     *  比较是否在两个时间之间
     * @param statDate          起始时间
     * @param endDate           结束时间
     * @param paramDate         要比较的时间
     * @param dayCompareType    是否是按日比较
     * @return
     */
    public static boolean isContains(Date statDate, Date endDate, Date paramDate, boolean dayCompareType) {
        if(dayCompareType) {
            Date beginTime = new Date(statDate.getTime());
            Date endTime = new Date(endDate.getTime());
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(beginTime);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.SECOND,0);
            calendar.set(Calendar.MINUTE,0);
            calendar.set(Calendar.MILLISECOND,0);
            beginTime = calendar.getTime();
            calendar = Calendar.getInstance();
            calendar.setTime(endTime);
            calendar.set(Calendar.HOUR_OF_DAY, 23);
            calendar.set(Calendar.SECOND,59);
            calendar.set(Calendar.MINUTE,59);
            calendar.set(Calendar.MILLISECOND,0);
            endTime = calendar.getTime();
            if(beginTime.before(paramDate) && endTime.after(paramDate)) {
                return true;
            } else {
                return false;
            }
        } else {
            if(statDate.before(paramDate) && endDate.after(paramDate)) {
                return true;
            } else {
                return false;
            }
        }
    }

    /**
     * 返回日期经过若干月后的日期(传入几位日期便返回多少位的格式)
     * @param dateStr
     * @param hkm int
     * @return String
     */
    public static String getDateStr(String dateStr, int hkm) {
        String st_return = "";
        boolean isEight = true;
        if(dateStr.split("-").length==3){
            isEight = false;
        } else {
            dateStr = dateStr.substring(0, 4) + "-" + dateStr.substring(4, 6) + "-" + dateStr.substring(6, 8);
        }
        try {
            DateFormat daf_date = DateFormat.getDateInstance(DateFormat.MEDIUM,
                    Locale.CHINA);
            daf_date.parse(dateStr);
            Calendar calendar = daf_date.getCalendar();
            calendar.add(Calendar.MONTH, hkm);
            String st_m = "";
            String st_d = "";
            int y = calendar.get(Calendar.YEAR);
            int m = calendar.get(Calendar.MONTH) + 1;
            int d = calendar.get(Calendar.DAY_OF_MONTH);
            if (m <= 9) {
                st_m = "0" + m;
            } else {
                st_m = "" + m;
            }
            if (d <= 9) {
                st_d = "0" + d;
            } else {
                st_d = "" + d;
            }
            if(isEight){
                st_return = y + "" + st_m + "" + st_d;
            }else {
                st_return = y + "-" + st_m + "-" + st_d;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return st_return;
    }

    /**
     * 格式化日期，计算日期根据添加类型和值
     * @param field
     * @param addValue
     * @param paramDate
     * @param parten
     * @return
     */
    public static String addDate(int field,int addValue, Date paramDate, String parten) {
        if(paramDate == null || StringUtils.isEmpty(parten)) {
            return getYYYYMMddHHmmss();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(paramDate);
        calendar.add(field, addValue);
        SimpleDateFormat sim = new SimpleDateFormat(parten);
        return sim.format(calendar.getTime());
    }

    /**
     * 将日期减一天
     * @param date
     * @return
     */
    public static Date getSubOneDay(Date date){
        if (null == date) {
            return null;
        }
        Calendar c1 = Calendar.getInstance();
        c1.setTime(date);
        c1.add(Calendar.DATE,-1);
        return c1.getTime();
    }

    /**
     * 将日期减n天
     * @param date
     * @return
     */
    public static Date getSubNDay(Date date,int n){
        if (null == date) {
            return null;
        }
        Calendar c1 = Calendar.getInstance();
        c1.setTime(date);
        c1.add(Calendar.DATE,-n);
        return c1.getTime();
    }


    /**
     * 将日期加n天
     * @param date
     * @return
     */
    public static Date getAddNDay(Date date,int n){
        if (null == date) {
            return null;
        }
        Calendar c1 = Calendar.getInstance();
        c1.setTime(date);
        c1.add(Calendar.DATE,n);
        return c1.getTime();
    }

    /**
     * 将日期加n个月
     * @param date
     * @return
     */
    public static Date getAddNMonth(Date date,int n){
        if (null == date) {
            return null;
        }
        Calendar c1 = Calendar.getInstance();
        c1.setTime(date);
        c1.add(Calendar.MONTH,n);
        return c1.getTime();
    }

    /**
     * 将日期加n个年
     * @param date
     * @return
     */
    public static Date getAddNYear(Date date,int n){
        if (null == date) {
            return null;
        }
        Calendar c1 = Calendar.getInstance();
        c1.setTime(date);
        c1.add(Calendar.YEAR,n);
        return c1.getTime();
    }

    /**
     * 获取日期加N分钟
     * @param date
     * @param minutes
     * @return
     */
    public static Date getAddNMinute(Date date, int minutes){
        if (null == date) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MINUTE, minutes);
        return c.getTime();
    }


    /**
     * 将日期加一天
     * @param date
     * @return
     */
    public static Date getAddOneDay(Date date){
        if (null == date) {
            return null;
        }
        Calendar c1 = Calendar.getInstance();
        c1.setTime(date);
        c1.add(Calendar.DATE,1);
        return c1.getTime();
    }

    /**
     * 日期转yyyyMMdd格式
     * @param date
     * @return
     */
    public static Date formatDateByYYYYMMdd(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat(str_yyyy_MM_dd);
        String str = DateUtil.formatDate(date, str_yyyy_MM_dd);
        try {
            return formatter.parse(str);
        } catch (ParseException e) {
            logger.error("日期格式转换异常!");
        }
        return null;
    }

    /**
     * 判断传入日期所属年份是否是闰年
     * @param checkDate
     * @return
     */
    public static boolean checkLeapYear(Date checkDate){
        boolean flag = false;
        if (checkDate!=null){
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(checkDate);
            int year = calendar.get(Calendar.YEAR);
            if (year%4==0){
                if (year%100==0){
                    if (year%400==0){
                        flag = true;
                    }
                } else{
                    flag = true;
                }
            }
        }
        return flag;
    }

    /**
     * 计算指定日期所属月份的最后一天
     * @param date
     * @return
     */
    public static int getMonthLastDay(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // 月份从0开始
        int month = calendar.get(Calendar.MONTH);
        switch (month){
            case 1:
                if (checkLeapYear(date)){
                    return 29;
                }
                return 28;
            case 3:
            case 5:
            case 8:
            case 10: return 30;
            default: return 31;
        }
    }

    /**
     * 获取指定月份指定某一天的日期
     * @param month 指定月份的日期，可以为当月任意一天，一般传当月第一天
     * @param specifiedDay 指定某一天（会按就近原则自动规范化到1到31），若当月没有该天，则取当月的最后一天
     * @return
     */
    public static Date getSpecifiedMonthDay(Date month, int specifiedDay){
        Calendar temp = Calendar.getInstance();
        temp.setTime(month);
        int lastDay = getMonthLastDay(month);
        if (specifiedDay<lastDay){
            temp.set(Calendar.DAY_OF_MONTH, specifiedDay);
        } else{
            temp.set(Calendar.DAY_OF_MONTH, lastDay);
        }
        return temp.getTime();
    }

    /**
     * 获取当前小时（24小时制）
     */
    public static int getCurrentHourFor24() {
        Calendar cal = Calendar.getInstance();
        // HOUR_OF_DAY用于24小时时钟
        // HOUR用于12小时时钟
        return cal.get(Calendar.HOUR_OF_DAY);
    }

    /**
     *  获取当前日期相差intervalYear年的日期
     * @param currentDate
     * @param intervalYear
     * @return
     */
    public static Date getIntervalTime(Date currentDate,int intervalYear) {
        Calendar calendar = null;
        Date day=null;
        try {
            calendar = Calendar.getInstance();//得到日历
            calendar.setTime(currentDate);
            calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) +intervalYear);
            day = calendar.getTime();

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return day;
    }

    /**
     * 获取时间差多少分钟
     * @param date
     */
    public static long getDateTimeDifference(String date) {
        long newTimeInMilli = 0L;
        try {
            Calendar calendar1 = Calendar.getInstance();
            Calendar calendar2 = Calendar.getInstance();
            calendar1.setTime(PluginDateUtils.parseStr2Date(date));
            System.out.println("开始时间：" + PluginDateUtils.formatDate(calendar1.getTime(), PluginDateUtils.str_yyyy_MM_dd_HH_mm_ss));
            calendar2.setTime(new Date());
            System.out.println("结束时间：" + PluginDateUtils.formatDate(calendar2.getTime(), PluginDateUtils.str_yyyy_MM_dd_HH_mm_ss));

            long timeInMilli1 = calendar1.getTimeInMillis();
            long timeInMilli2 = calendar2.getTimeInMillis();

            long timeInMilli = timeInMilli1 - timeInMilli2;
            newTimeInMilli = timeInMilli / 60 / 1000;
            System.out.println("相差：" + newTimeInMilli + "分钟");
        } catch (Exception e) {
            System.out.println("计算时间差失败，异常信息：" + e.getMessage());
        }
        return newTimeInMilli;
    }

}
